<?php

class ControllerModuleLekoService extends Controller
{
    public function index()
    {
        /*
        $this->load->model('leko/service');
        $this->test->show($this->model_leko_service->webServiceMysqlTest(310));
        */

        $this->load->language('module/lekoService');
        $this->load->model('leko/service');
        $this->load->model('checkout/cart');
        $this->load->helper('html');

        $this->data['articles']='';
        $this->data['vendors']='';
        $this->data['error']='';
        $this->data['articles_title']='';
        $this->data['alts_title']='';
        $this->data['notOriginals_title']='';
        $this->data['searchNum']='';

        if(isset($this->session->data['art'])){
            $this->data['searchNum']=$num=$this->session->data['art'];
            $vendor=isset($this->session->data['vendor']) ? $this->session->data['vendor'] : '';

            if($articles=$this->model_leko_service->getArticles($num,$vendor)){
                $this->data['articles']=$this->model_leko_service->getHtmlArticles($articles);
                $this->data['vendors']=$this->model_leko_service->getHtmlVendors($articles->vendors);
                $this->data['error']=$articles->error;
                $this->data['articles_title']=$this->language->get('articles_title');
                $this->data['alts_title']=$this->language->get('alts_title');
                $this->data['notOriginals_title']=$this->language->get('notOriginals_title');
            }
        }

        $this->data['actionUrl']=$this->model_checkout_cart->getActionUrl();
        $this->data['button_cart']=$this->language->get('button_cart');
        $this->data['text_article_search']=$this->language->get('text_article_search');
        $this->data['text_model_search']=$this->language->get('text_model_search');
        $this->data['text_consult_search']=$this->language->get('text_consult_search');
        $this->data['text_choose_category']=$this->language->get('text_choose_category');
        $this->data['table_left_image']=HTTP_IMAGE.'data/tables-left-square.gif';
        $this->data['table_left_image_dark']=HTTP_IMAGE.'data/tables-left-square-dark.gif';
        $this->data['table_left_image_alt']='table left image';
        $this->data['selectOptons']=array();
        $this->data['searchModel']='';
        $this->data['inquiry']=$this->language->get('text_inquiry');
        $this->data['phone']=$this->language->get('text_phone');
        $this->data['mail']=$this->language->get('text_mail');
        $this->data['inquiry_value']='';
        $this->data['phone_value']='';
        $this->data['mail_value']='';
        $this->data['notice']=$this->language->get('error_message');
        $this->data['search_button']=getAjaxButton(array(
            'button_text'=>$this->language->get('search_button'),
            'div_class'=>'button',
            'span_class'=>'button',
        ));
        $this->data['loading_image']=getLoadingImage(array('
            img_src'=>HTTP_IMAGE.'data/loadingIcon.gif',
            'div_id'=>'search-loading',
            'div_class'=>'loading',
            'img_id'=>'loading',
        ));

        if($this->data['searchNum'] && !$this->data['vendors'] && $this->data['searchNum'] && !$this->data['articles'] && !$this->data['error']) $this->data['error']=$this->language->get('error_message');

        $this->data['selectOptions']=array();
        $this->load->model('catalog/category');
        if($categories=$this->model_catalog_category->getCategories(71)){
            foreach($categories as $category){
                $this->data['selectOptions'][]=array(
                    'name'=>$category['name'],
                    'value'=>$category['category_id'],
                );
            }
        }

        $this->data['display_style']=!$this->cart->hasProducts() ? 'style="display:none;"' : '';
        $this->template='default/template/module/lekoService.tpl';
        $this->children=array(
            'checkout/cartInfo',
            'checkout/cartTotal',
            'checkout/cartButtons',
        );

        $this->response->setOutput($this->render());
    }

    public function search()
    {
        $json=array();
        $json['result']='';
        $json['error']='';

        if($request=$this->request->post){
            /*
            if(isset($request['art'])){
                $this->session->data['art']=$request['art'];
                unset($this->session->data['vendor']);
            }
            */
            //if(isset($request['vendor'])) $this->session->data['vendor']=$request['vendor'];

            /*
            $art=isset($this->session->data['art']) ?  $this->session->data['art'] : 0;
            $vendor=isset($this->session->data['vendor']) ? $this->session->data['vendor'] : '';
            */

            $art=isset($request['art']) ?  $request['art'] : 0;
            $vendor=isset($request['vendor']) ? $request['vendor'] : '';

            if($art){
                $this->load->language('module/lekoService');
                $this->load->model('leko/service');

                if($articles=$this->model_leko_service->getArticles($art, $vendor)){
                    if($articles->vendors) $json['result']=$this->model_leko_service->getHtmlVendors($articles->vendors);
                    else $json['result']=$this->model_leko_service->getHtmlArticles($articles);
                    $json['error']=$articles->error;
                }
            }

            if(!$json['result']){
                //unset($this->session->data['art']);
                if($art && !$json['error']) $json['error']=1;
            }
        }

        $this->response->setOutput(json_encode($json));
    }

    public function noFunction($noParams=null){
        //nothing to do;
        unset($noParams);
        return;
    }
}